package cn.iocoder.yudao.module.service.controller.admin.stepconfig;

import cn.iocoder.yudao.module.service.controller.admin.sceneconfig.vo.SceneConfigPageReqVO;
import cn.iocoder.yudao.module.service.controller.admin.sceneconfig.vo.SceneConfigRespVO;
import cn.iocoder.yudao.module.service.dal.dataobject.sceneconfig.SceneConfigDO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.service.controller.admin.stepconfig.vo.*;
import cn.iocoder.yudao.module.service.dal.dataobject.stepconfig.StepConfigDO;
import cn.iocoder.yudao.module.service.service.stepconfig.StepConfigService;

@Tag(name = "管理后台 - 步骤配置")
@RestController
@RequestMapping("/service/step-config")
@Validated
public class StepConfigController {

    @Resource
    private StepConfigService stepConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建步骤配置")
    @PreAuthorize("@ss.hasPermission('service:step-config:create')")
    public CommonResult<Long> createStepConfig(@Valid @RequestBody StepConfigSaveReqVO createReqVO) {
        return success(stepConfigService.createStepConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新步骤配置")
    @PreAuthorize("@ss.hasPermission('service:step-config:update')")
    public CommonResult<Boolean> updateStepConfig(@Valid @RequestBody StepConfigSaveReqVO updateReqVO) {
        stepConfigService.updateStepConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除步骤配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('service:step-config:delete')")
    public CommonResult<Boolean> deleteStepConfig(@RequestParam("id") Long id) {
        stepConfigService.deleteStepConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得步骤配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('service:step-config:query')")
    public CommonResult<StepConfigRespVO> getStepConfig(@RequestParam("id") Long id) {
        StepConfigDO stepConfig = stepConfigService.getStepConfig(id);
        return success(BeanUtils.toBean(stepConfig, StepConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得步骤配置分页")
    @PreAuthorize("@ss.hasPermission('service:step-config:query')")
    public CommonResult<PageResult<StepConfigRespVO>> getStepConfigPage(@Valid StepConfigPageReqVO pageReqVO) {
        PageResult<StepConfigDO> pageResult = stepConfigService.getStepConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StepConfigRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得场景配置列表")
    @PreAuthorize("@ss.hasPermission('service:step-config:query')")
    public CommonResult<List<StepConfigRespVO>> getStepConfigList(@RequestParam("sceneId") Long sceneId) {
        StepConfigPageReqVO pageReqVO = new StepConfigPageReqVO();
        pageReqVO.setSceneId(sceneId);
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StepConfigDO> pageResult = stepConfigService.getStepConfigPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, StepConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出步骤配置 Excel")
    @PreAuthorize("@ss.hasPermission('service:step-config:export')")
    @OperateLog(type = EXPORT)
    public void exportStepConfigExcel(@Valid StepConfigPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StepConfigDO> list = stepConfigService.getStepConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "步骤配置.xls", "数据", StepConfigRespVO.class,
                BeanUtils.toBean(list, StepConfigRespVO.class));
    }

}